package com.sgb.kjwl.view.ui.fragment.demandHall;

import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentDemandhallListBinding;
import com.sgb.kjwl.di.home.DaggerHomeFragComponent;
import com.sgb.kjwl.di.home.HomeFragModule;
import com.sgb.kjwl.model.entity.DemandHallListEntity;
import com.sgb.kjwl.model.entity.EnquiryTypeEntity;
import com.swgk.core.event.EventConstant;
import com.sgb.link.event.LeaseEvent;
import com.sgb.kjwl.view.event.LoginEvent;
import com.sgb.kjwl.view.ui.adapter.carrierDemand.DemandHallAdapter;
import com.sgb.kjwl.view.widget.Enquirytypepopwindow;
import com.sgb.kjwl.viewmodel.demand.DemandHallTabModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.xrecyclerview.XRecyclerView;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;

/**
 * 需求大厅tab页签数据列表页
 */
public class DemandHallListFragment extends AppFragment {

    private static final String DEMAND_TYPE = "demand_type";
    private String goosType;
    private FragmentDemandhallListBinding binding;
    List<EnquiryTypeEntity> enquirylist = new ArrayList<>();
    List<EnquiryTypeEntity> sortlist = new ArrayList<>();
    List<EnquiryTypeEntity> userlist = new ArrayList<>();
    private String selectType;
    private String enquiryid = "0";
    private String userid = "";
    private String sortid = "2";
    private boolean isLoadMore = false;
    private int page = 1;
    @Inject
    DemandHallTabModel demandhalltabmodel;
    private Enquirytypepopwindow enquirytypepopwindow;
    private Enquirytypepopwindow userTypepopwindow;
    private Enquirytypepopwindow sortTypepopwindow;
    private DemandHallAdapter demandHallAdapter;
    private String searchName = "";
    private String tabName = "";
    private boolean isCreate = false;
    private boolean init = true;
    private boolean isVisible;
    private String tabNames;
    private String shoppingNo;

    public static DemandHallListFragment newInstance(String goodsType) {
        DemandHallListFragment fragment = new DemandHallListFragment();
        Bundle args = new Bundle();
        args.putString(DEMAND_TYPE, goodsType);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerHomeFragComponent.builder()
                .appComponent(appComponent)
                .homeFragModule(new HomeFragModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
       isVisible = isVisibleToUser;
        if (!isCreate) {
            return;
        }
        if (isVisibleToUser) {
            if (init) {
                init = false;
                if (getArguments() != null) {
                    goosType = getArguments().getString(DEMAND_TYPE);
                }
                tabName=goosType;
                initview();
                initdata();
            }
        }
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isCreate = true;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_demandhall_list, container, false);
        binding.setViewModel(demandhalltabmodel);
        return binding.getRoot();
    }

    private void initdata() {
        demandhalltabmodel.getEnquiryType();
        demandhalltabmodel.getUserType();
        demandhalltabmodel.getSortType();
        getdata();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
       if (init && isVisible) {
            if (!EventBus.getDefault().isRegistered(this)) {
                EventBus.getDefault().register(this);
            }
            if (getArguments() != null) {
                goosType = getArguments().getString(DEMAND_TYPE);
            }
            initview();
            initdata();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEvent(LeaseEvent event) {
        if (event.getMessageTag() == EventConstant.EVENT_SELECT_DEMANDHALL_TAB) {
            tabNames = (String) event.getMessageData();
        }
        if (event.getMessageTag() == EventConstant.EVENT_SELECT_DEMANDHALL) {
            searchName = (String) event.getMessageData();
        }
        tabName=tabNames;
        getdata();
    }
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEvent(LoginEvent event) {
        if (event.getMessageTag() == EventConstant.EVENT_SHOPPING_LIST_LOGIN) {
            SharedPreferenceUtil.getInstance().saveString("EVENT_SHOPPING_TYPE", "");
            demandhalltabmodel.showUrl(shoppingNo);
        }
    }

    private void initview() {

        binding.llStatus.setOnClickListener(this);
        binding.llUser.setOnClickListener(this);
        binding.llRank.setOnClickListener(this);
        binding.llArea.setOnClickListener(this);
        binding.llDistance.setOnClickListener(this);

        enquirytypepopwindow = new Enquirytypepopwindow(getActivity());
        userTypepopwindow = new Enquirytypepopwindow(getActivity());
        sortTypepopwindow = new Enquirytypepopwindow(getActivity());

        enquirytypepopwindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                if (null != key1) {
                    enquiryid = ((EnquiryTypeEntity) key1).getId();
                    binding.tvStatus.setText(((EnquiryTypeEntity) key1).getName());
                }
                getdata();
            }
        });
        userTypepopwindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                if (null != key1) {
                    userid = ((EnquiryTypeEntity) key1).getId();
                    binding.tvUser.setText(((EnquiryTypeEntity) key1).getName());
                }
                getdata();
            }
        });
        sortTypepopwindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                if (null != key1) {
                    sortid = ((EnquiryTypeEntity) key1).getId();
                    binding.tvRank.setText(((EnquiryTypeEntity) key1).getName());
                }
                getdata();
            }
        });
        if (demandHallAdapter == null) {
            demandHallAdapter = new DemandHallAdapter(getActivity(), null);
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.demandList.setLayoutManager(layoutManager);
        binding.demandList.setAdapter(demandHallAdapter);
        binding.demandList.setLoadingMoreEnabled(true);
        binding.demandList.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                getdata();
            }

            @Override
            public void onLoadMore() {
                isLoadMore = true;
                page = page + 1;
                demandhalltabmodel.getDemandHallList(userid, sortid, enquiryid, tabName, searchName, page);
            }
        });

        demandHallAdapter.setOnItemClick(new AdapterOnItemClick<DemandHallListEntity>() {
            @Override
            public void onItemClick(DemandHallListEntity demandhalllistentity, int position) {
                shoppingNo = demandhalllistentity.getEnquiryNo();
//                跳转详情
                demandhalltabmodel.showUrl(demandhalllistentity.getEnquiryNo());
            }
        });

    }

    private void getdata() {
        isLoadMore = false;
        page = 1;
        demandhalltabmodel.getDemandHallList(userid, sortid, enquiryid, tabName, searchName, page);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_status://状态选择
                if (null == enquirylist || enquirylist.size() == 0)
                    return;
                enquirytypepopwindow.showPopwindow(binding.selectLayout, 0, 0);
                enquirytypepopwindow.setData(enquirylist);
                break;
            case R.id.ll_user://用户选择
                if (null == userlist || userlist.size() == 0)
                    return;
                userTypepopwindow.showPopwindow(binding.selectLayout, 0, 0);
                userTypepopwindow.setData(userlist);
                break;
            case R.id.ll_area://地区选择
                break;
            case R.id.ll_distance://距离选择
                break;
            case R.id.ll_rank://排序选择
                if (null == sortlist || sortlist.size() == 0)
                    return;
                sortTypepopwindow.showPopwindow(binding.selectLayout, 0, 0);
                sortTypepopwindow.setData(sortlist);
                break;
        }
    }

    //返回状态数据
    public void getEnquirtTypeList(List<EnquiryTypeEntity> enquiryTypelist) {
        enquirylist.clear();
        enquirylist.addAll(enquiryTypelist);
    }

    //返回用户数据
    public void getUserTypeList(List<EnquiryTypeEntity> userTypelist) {
        userlist.clear();
        userlist.addAll(userTypelist);
    }

    //返回排序数据
    public void getSortTypeList(List<EnquiryTypeEntity> sortTypelist) {
        sortlist.clear();
        sortlist.addAll(sortTypelist);
    }

    //获取列表数据
    public void setDemandHallList(List<DemandHallListEntity> demandHallListEntityList) {
        binding.demandList.refreshComplete();
        if (isLoadMore) {
            binding.demandList.loadMoreComplete();
            if (demandHallListEntityList.size() > 0) {
                demandHallAdapter.addDatas(demandHallListEntityList);
            } else {
                binding.demandList.setLoadingMoreEnabled(false);
            }
            binding.demandList.setVisibility(View.VISIBLE);
        } else {
            binding.demandList.refreshComplete();
            if (demandHallListEntityList.size() > 0) {
                binding.demandList.setVisibility(View.VISIBLE);
                binding.emptyView.setVisibility(View.GONE);
                demandHallAdapter.setDatas(demandHallListEntityList);
                binding.demandList.setLoadingMoreEnabled(true);
            } else {
                demandHallAdapter.setDatas(null);
                binding.demandList.setVisibility(View.GONE);
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
